package org.outpoint.service;

import java.util.Date;
import java.util.HashMap;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.PasswordSecurity;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.util.JsonUtil;
import org.outpoint.domain.UserPassword;
import org.outpoint.repository.UserPasswordRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <p>Title: UserPasswordService</p>
 * <p>Description: 外部系统密码修改服务类</p>
 * <p>Company: 长沙东勤信息科技发展有限公司</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2019年9月24日 下午10:48:13
 */
@RestController
@RequestMapping("/outPointPassword")
public class UserPasswordService {
	
	private EntityManager entityManager;	
	
	@PersistenceContext(unitName="password")
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;
	}
	@Autowired
	private UserPasswordRepo baseRepo ;	
	
	
	private void setService(UserPasswordRepo baseRepo){
		this.baseRepo = baseRepo;		
	}	
	
	/**
	 * 保存用户密码
	 * @param id
	 */
	@RequestMapping("/resetPassword.action")
	public RetDomain resetPassword(@RequestBody String body){
		
		@SuppressWarnings("rawtypes")
		HashMap map =  JsonUtil.bodyToObj(body, HashMap.class);
		
		if(map != null && map.containsKey("id")){
			String _id = map.get("id").toString();
			UserPassword newObj = JsonUtil.bodyToObj(body, UserPassword.class);
			newObj.setYhid(_id);
			return this.resetPassword(newObj);
		}else{
			RetDomain r = RetDomain.errorDomain("密码设置失败");
			r.setCode("-1");
			return r;
		}
		
		
	}
	
	/**
	 * 保存用户密码
	 * @param id
	 */
	@RequestMapping("/reset.action")
	public RetDomain resetPassword(@RequestParam String uid,@RequestParam String oldPassword,@RequestParam String newPassword){
		UserPassword newObj = new UserPassword();
		newObj.setYhid(uid);
		newObj.setOldPassword(oldPassword);
		newObj.setNewPassword(newPassword);
		return this.resetPassword(newObj);
	}
	/*
	 * 重置密码
	 */
	private RetDomain resetPassword(UserPassword newObj){
		
		RetDomain r = RetDomain.errorDomain("密码修改失败");
		
		
		UserPassword oldObj = null;
		//判断用户信息是否为空
		if(newObj != null ){
			//判断用户代码是否为空
			if(newObj.getYhid() == null){
				r.setCode("-1");
				return r;
			}
			
			//判断新密码是否为空
			if(newObj.getNewPassword() == null){
				r.setCode("-2");
			}
			
			//查询数据库中密码信息
			oldObj = baseRepo.findOne(newObj.getYhid());
			
			//判断用户是否存在
			if(oldObj !=null && oldObj.getYhid()!=null){
				//判断输入的原密码是否正确
				
				String oPass = oldObj.getYhmm();
				
				//原密码是否为空
				if(	(oPass == null) ||
						//输入的原密码是否正确
						(this.comparePassword(newObj.getYhid().toString(),oPass, newObj.getOldPassword()))){
					
					//加密新密码
					newObj.setYhmm(this.encryptPassword(newObj.getYhid().toString(),newObj.getNewPassword()));
					
					r = RetDomain.successDomain("密码设置成功");
					r.setCode("1");
				}else{
					//原密码校验失败
					r.setCode("-3");
					return r;
				};
			}else{					
				//用户不存在
				r.setCode("-1");
				return r;
			}			
			//判断原密码输入是否正确			
		}
		
		//更新密码刷新日期
		newObj.setMmsxrq(new Date());
		baseRepo.save(newObj);		
		return r;
	}
	
	/*
	 * 初始化密码
	 */
	@RequestMapping("/initPwd.action")
	public RetDomain initPwd(@RequestParam(value="id",required=false) String id){		
		
		RetDomain r = RetDomain.errorDomain("初始化密码失败");
		if(id != null ){
			
			//查找用户是否存在
			String _id = id;
			UserPassword newObj = baseRepo.findOne(_id);
			
			if(newObj!=null  &&newObj.getYhid()!=null ){
				//用户存在,生成初始密码
				newObj.setYhmm(this.encryptPassword(_id, "123456"));
				
				//重置密码刷新时间
				newObj.setMmsxrq(new Date());
				baseRepo.save(newObj);
				
				r = RetDomain.successDomain("初始化密码成功");
			}else{
				//用户不存在，设置错误码，返回
				r.setCode("-2");
			}
			
		}else{
			r.setCode("-1");
		}
		return r;
	}
	
	/*
	 * 比较原密码输入是否正确
	 */
	private boolean comparePassword(String userid,String oldPassword,String newPassword){
		String ePass = this.encryptPassword(userid, newPassword);
		
		oldPassword = oldPassword==null?"":oldPassword;
		if(oldPassword.compareTo(ePass)==0){
			return true;
		}
		return false;
	}
	/*
	 * 密码加密
	 */
	private String encryptPassword(String userid,String plainPwd){
		
		return PasswordSecurity.generateMD5(userid,plainPwd,true);
	}

}
